package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@Tag(name = "01-用户管理接口",description = "用户管理接口")
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @Operation(summary = "添加用户")
    @ApiOperationSupport(order = 10)
    public JsonResult addUser(@RequestBody@Validated UserRegParam userRegParam) {
        log.debug("添加用户："+userRegParam);
        String username = userRegParam.getUsername();
        int count = userMapper.selectCountByUsername(username);
        if (count>0){
            return  new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("添加数据库:"+user);
        int i=userMapper.insertUser(user);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATTION_FAILED);
    }

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession  session) {
        log.debug("用户登录："+userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }
    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }

    @GetMapping("logout")
    @Operation(summary = "退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }

    @GetMapping("reg2")
    public JsonResult addUser2(HttpServletRequest  request) {
       String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        System.out.println( username+password+email+phone);
    String addr=request.getRemoteAddr( );
    String method=request.getMethod();
    Cookie[]cookies=request.getCookies();
    HttpSession session=request.getSession();
    return JsonResult.ok();
    }
    }


